#region Imported Namespaces
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using UserContext = PHSRAG.Insight21.Context.UserContext;
using Annunciator = PHSRAG.WebControls.Annunciator;
using Authenticator = PHSRAG.Security.Authenticator;
using BusinessObjects = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using Policy = PHSRAG.Insight21.Policy;
using DataAccess = PHSRAG.Utility.DataAccess;
#endregion

namespace PHSRAG.Insight21
{
    #region Class Login
    /// <summary>
    /// Summary description for Login.
    /// </summary>
    public class Login
    {


        /// <summary>
        /// Loads a UserContext object based on the passed in domain username,
        /// checks that the user is active and has been granted at least one
        /// role, and adds the information  a session variable.  If the user is
        /// inactive or does not have any roles a system exception is thrown.
        /// </summary>
        /// <param name="userName">Domain user's username</param>
        public static void LoadUserContext(string userName, PHSRAG.Insight21.Policy.ICachePolicy cachePolicy)
        {
           
                string connectionString = Utility.DataAccess.GetConnectionString();

                //get the user's context
                UserContext userContext = new UserContext(cachePolicy);

                userContext.Load(connectionString, userName);

                //@@@revisit this b/c we should be comparing the application's bitvalue
                //with the user's IsActiveBitMask's bit value's...if there is a match then
                //the user is active for the application.
                if (userContext.IsActiveBitMask <= 1)
                {
                    throw new Exception("Unable to log into Insight.  The user is inactive.");
                }
                //@@@revisit this to match on actual roles defined in the db
                if (userContext.ModuleBitmask <= 1)
                {
                    throw new Exception("Unable to log into Insight.  You do not have access to at least one module.");
                }

                //now save a new session history entry for the application user
                userContext.CurrentSessionStartDate = DateTime.Now;
                userContext.Save(connectionString);

                //save the UserContext in the current cache (using the cache policy).
                userContext.Cache();
            
           
        }


        void RedirectUser(string mid)
        {
            /* mid = 1 indicates that the user is inactive in Insight
             * mid = 2 indicates that the user is not a SYSADMIN in Insight
             * mid = 3 indicates that the user is not a SYSADMIN in Insight
             * mid = 4 indicates that the user is not a SYSADMIN in Insight
                     */
            HttpContext.Current.Response.Redirect("login/logout.html?mid=" + mid, true);
        }

        public static void LoadSysUserContext(string userName,string businessUser,  PHSRAG.Insight21.Policy.ICachePolicy cachePolicy)
        {
          
                string connectionString = Utility.DataAccess.GetConnectionString();

                UserContext sysAdminUserContext = new UserContext(cachePolicy);

                sysAdminUserContext.Load(connectionString, userName);

                //@@@revisit this b/c we should be comparing the application's bitvalue
                //with the user's IsActiveBitMask's bit value's...if there is a match then
                //the user is active for the application.
                if (sysAdminUserContext.IsActiveBitMask <= 1)
                {
                    
                    throw new Exception("Unable to log into Insight.  The Sys Admin User is inactive.");
                }

                if (!sysAdminUserContext.IsUserSysAdmin)
                {

                    /* mid = 2 indicates that the user is not a SYSADMIN in Insight
                     */
                    //HttpContext.Current.Response.Redirect("login/logout.html?mid=2", true);
                    throw new Exception("Unable to log into Insight.  The Sys Admin User specified is not an Insight System Administrator.");
                }


                //get the user's context
                UserContext userContext = new UserContext(cachePolicy);

                userContext.Load(connectionString, businessUser);

                //@@@revisit this b/c we should be comparing the application's bitvalue
                //with the user's IsActiveBitMask's bit value's...if there is a match then
                //the user is active for the application.
                if (userContext.IsActiveBitMask <= 1)
                {
                    throw new Exception("Unable to log into Insight.  The user is inactive.");
                }
                //@@@revisit this to match on actual roles defined in the db
                if (userContext.ModuleBitmask <= 1)
                {
                    throw new Exception("Unable to log into Insight.  You do not have access to at least one module.");
                }

                //now save a new session history entry for the application user
                //			userContext.CurrentSessionStartDate = DateTime.Now;
                //			userContext.Save(connectionString);

                //save the UserContext in the current cache (using the cache policy).
                userContext.Cache();
               
            }
           
     

    }
    #endregion
}
